require(['init'], function () {
/**
 * @author Levi
 * @url http://levi.cg.am/archives/3467
 */
    //获取课程

    var $EditAdd = $('<div class="formList Nformstyle" id="J_enroll_wrap" >' +
                         '<input type="hidden" name="id">' +
                     '<div class="formLi1 Snameinput">' +
                        '<div class="formTitle1  ">' +
                            '<span>'+i18next.t('index.lessontype')+'：</span>' +
                        '</div>' +
                        '<div class="formContent1">' +
                            '<input class="form-control a1 focus studentname" data-formcheck="length" type="text" name="name">' +
                        '</div>' +
                   ' </div>' +
                  //   '<div class="formLi1 Snameinput">' +
                  //       '<div class="formTitle1  ">' +
                  //           '<span>课程颜色：</span>' +
                  //       '</div>' +
                  //           '<div class="formContent1 Color" style="position:relative">' +
                  //           '<input class="form-control a1 focus" readonly  type="text" placeholder=""  id="lineColor"  name="mycolor"  hx="#c00">' +
                  //       '</div>' +
                  //  '</div>' +
                   '<div class="formLi1 Snameinput">' +
                        '<div class="formTitle1  ">' +
                            '<span>'+i18next.t('popupbox.remark')+'：</span>' +
                        '</div>' +
                        '<div class="formContent1">' +
                            '<input class="form-control a1 focus studentname"  type="text" name="remark" >' +
                        '</div>' +
                    '</div>');
     function getCouerse() {
        ajaxMethod({
            url: '/api/scheduletype',
            type:'get',
            success: function (data) {
              console.log(data);
                var $CourseColorTable = $('#CourseColor').html('');
                var $thead = $('<thead>\
                    <tr style="border:1px solid #ccc;border-botttom:none">\
                        <th>'+i18next.t('table.no')+'</th>\
                        <th>'+i18next.t('table.lessontype')+'</th>\
                        <th>'+i18next.t('table.remark')+'</th>\
                        <th>'+i18next.t('table.handle')+'</th>\
                    </tr>\
               </thead>').appendTo($CourseColorTable);
                var $tbody = $('<tbody></tbody>').appendTo($CourseColorTable);
                $.each(data, function (index, item) {
                    var id = item.id;
                    var $enroll = $('<tr class="enroll" data-uid="' + id + '"  ></tr>').appendTo($tbody);
                    $enroll.append('<td>1</td>')
                    $enroll.append('<td>' + item.name + '</td>')
                    //var $color = $('<td> <input  type="text"  value=' + item.colorvalue + ' style="width:60px;border:none;border:1px solid #fff;background:blue;color:transparent"/></td>').appendTo($enroll)
                    $enroll.append('<td>'+item.remark+'</td>')
                    var $Edittd = $("<td></td>").appendTo($enroll);
                    var $Edit = $('<a class="btn btn-primary btn-sm btn-inverse">'+i18next.t('table.compile')+'</a>').appendTo($Edittd);
                    var $delete = $('<a class="btn btn-danger btn-sm btn-inverse" style="margin-left:15px;">'+i18next.t('table.delete')+'</a>').appendTo($Edittd);
                    $delete.on('click', function () {
                        //弹出层
                        var popUpBox = new PopUpBox({
                            type: 'dom',
                            style: 'default contentScroll',
                            submitBtnType: 'confirm',
                            titleText: i18next.t('popupbox.warmprompt'),//温馨提示
                            show: true,
                            content: '<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.del')+'</div>',//您确定要删除吗
                            callback: {
                                yes: function (e) {
                                    $.ajax({
                                        url: "/api/scheduletype/" + id,
                                        type: "delete",
                                        success: function () {
                                            getCouerse();

                                        }
                                    })
                                }
                            },
                            position: {
                                width: 448,
                                height: 226
                            }
                        })


                    })
                  //  var hxcolor = $color.find('input').val();
                    //$color.find('input').css('background', hxcolor);
                    $Edit.on("click", function () {
                      loadEnrollData(id);
                      // $EditAdd.find('input[name="mycolor"]').iColor({ 'x': 10, 'y': -50 });
                      //   $EditAdd.find('#lineColor').iColor(function (hx) {
                      //       // console.log('自定义回调：去掉内容');
                      //       this.val('').css('background', '#' + hx);
                      //   });
                         var popUpBox = new PopUpBox({
                            type: 'dom',
                            style: 'default contentScroll',
                            submitBtnType: 'confirm',
                             titleText: i18next.t('popupbox.editcontent'),//请编辑内容
                            show: true,
                            content: $EditAdd,
                            callback: {
                                yes: function (e) {
                                  bindSubmit();
                                  window.location.reload();

                                }
                            },
                            position: {
                                width: 396,
                                height:285
                            }
                        })


                    })
                })
                var len = $('table tr').length;
                for (var i = 1; i < len; i++) {
                  $('table tr:eq(' + i + ') td:first').text(i);
                }


            }


        })
    }
    getCouerse();

    //点击添加学生
    $("#CourserColor").on('click', function () {
      if($EditAdd.find('input').val()!=""){
        $EditAdd.find('input').val("");
       }
        //$EditAdd.find('input[name="mycolor"]').iColor({ 'x': 10, 'y': -50 });
        // $EditAdd.find('#lineColor').iColor(function (hx) {
        //     // console.log('自定义回调：去掉内容');
        // this.val('').css('background', '#' + hx);
        // });
        var popUpBox = new PopUpBox({
            type: 'dom',
            style: 'default contentScroll',
            submitBtnType: 'confirm',
            titleText:i18next.t('popupbox.pleasecontent'),//请填写内容
            show: true,
            content: $EditAdd,
            callback: {
                yes: function (e) {
                    if ($EditAdd.find("input[type='text']").val() != "") {
                      //var coursetypeuid = $EditAdd.find('[name=coursetypeuid]').val()
                      var name = $EditAdd.find('[name=name]').val()
                      var remark = $EditAdd.find('[name=remark]').val()
                      //var colorvalue1 = $EditAdd.find('[name=mycolor]').attr('hx')
                      //var colorvalue = $EditAdd.find('[name=mycolor]').val()
                    //  var colorvalue2 = '#' + colorvalue1;

                      var Studentroll = {
                          name: name,
                        //  colorvalue: colorvalue2,
                          remark:remark
                      };
                      ajaxMethod({
                          url: '/api/scheduletype',
                          type: 'post',
                          data: Studentroll,
                          success: function (data) {
                              window.location.reload();
                          }
                      });

                    } else {
                        return false;
                    }
                }
            },
            position: {
                width: 396,
                height: 285
            }
        })

    })
   function loadEnrollData(id, callback) {
        ajaxMethod({
            url: '/api/scheduletype/' + id,
            dataType: 'json',
            success: function (data) {
                console.log(data);
                //data = JSON.parse(data)
                $EditAdd.find('[name=id]').val(data.id);
                $EditAdd.find('[name=name]').val(data.name);
                $EditAdd.find('[name=remark]').val(data.remark);
                // $EditAdd.find('[name=mycolor]').attr("hx", data.colorvalue);
                // $EditAdd.find('[name=mycolor]').css("background",data.colorvalue);
               (typeof callback == 'function') && callback()
            }
        })
    }

    function bindSubmit(data) {

        if (FormCheck.childrenInputCheck($EditAdd.find('#J_enroll_wrap'))) {
            var id = $EditAdd.find('[name=id]').val()
            var name = $EditAdd.find('[name=name]').val()
            var remark = $EditAdd.find('[name=remark]').val()
            //var colorvalue1 = $EditAdd.find('[name=mycolor]').attr('hx')
            //var colorvalue = $EditAdd.find('[name=mycolor]').val()
            //var colorvalue2 = '#' + colorvalue1;

            var Studentroll = {
                //id: id,
                name: name,
                //colorvalue: colorvalue2,
                remark:remark
              }
            if (id!=undefined) {
                ajaxMethod({
                    url: '/api/scheduletype/'+id,
                    type: 'put',
                    data: $.extend(Studentroll, { id:id }),
                    success: function (data) {

                        //alert('修改成功')

                    }
                });

            }
         }
    }

})
